class ImportantDay {
  final String id;
  final String title;
  final String description;
  final DateTime date;
  final String category; // 纪念日、生日、节日等
  final String emoji;
  final bool isRecurring; // 是否每年重复
  final String? note; // 备注信息
  final bool isRead; // 是否已读
  final DateTime? readAt; // 标记已读的时间

  ImportantDay({
    required this.id,
    required this.title,
    required this.description,
    required this.date,
    required this.category,
    required this.emoji,
    this.isRecurring = false,
    this.note,
    this.isRead = false,
    this.readAt,
  });

  // 计算距离该日期还有多少天
  int get daysUntil {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    
    DateTime targetDate;
    if (isRecurring) {
      // 如果是每年重复的日期，计算今年的日期
      targetDate = DateTime(now.year, date.month, date.day);
      // 如果今年的日期已经过了，计算明年的
      if (targetDate.isBefore(today)) {
        targetDate = DateTime(now.year + 1, date.month, date.day);
      }
    } else {
      targetDate = DateTime(date.year, date.month, date.day);
    }
    
    return targetDate.difference(today).inDays;
  }

  // 是否是今天
  bool get isToday => daysUntil == 0;

  // 是否已经过去
  bool get isPast => daysUntil < 0;

  // 获取显示文本
  String get displayText {
    if (isToday) return '今天';
    if (isPast) return '已过去 ${-daysUntil} 天';
    return '还有 $daysUntil 天';
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'date': date.toIso8601String(),
      'category': category,
      'emoji': emoji,
      'isRecurring': isRecurring,
      'note': note,
      'isRead': isRead,
      'readAt': readAt?.toIso8601String(),
    };
  }

  factory ImportantDay.fromJson(Map<String, dynamic> json) {
    return ImportantDay(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      date: DateTime.parse(json['date']),
      category: json['category'],
      emoji: json['emoji'],
      isRecurring: json['isRecurring'] ?? false,
      note: json['note'],
      isRead: json['isRead'] ?? false,
      readAt: json['readAt'] != null ? DateTime.parse(json['readAt']) : null,
    );
  }

  // 创建已读副本
  ImportantDay copyWith({
    String? id,
    String? title,
    String? description,
    DateTime? date,
    String? category,
    String? emoji,
    bool? isRecurring,
    String? note,
    bool? isRead,
    DateTime? readAt,
  }) {
    return ImportantDay(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      date: date ?? this.date,
      category: category ?? this.category,
      emoji: emoji ?? this.emoji,
      isRecurring: isRecurring ?? this.isRecurring,
      note: note ?? this.note,
      isRead: isRead ?? this.isRead,
      readAt: readAt ?? this.readAt,
    );
  }
}